package cn.aitstone.service.service.impl;

import cn.aitstone.basic.service.impl.BaseServiceImpl;
import cn.aitstone.basic.utils.BaiduAuditUtils;
import cn.aitstone.basic.utils.ImgCensor;
import cn.aitstone.service.domain.Product;
import cn.aitstone.service.domain.ProductDetail;
import cn.aitstone.service.domain.ProductOnlineAuditLog;
import cn.aitstone.service.mapper.ProductDetailMapper;
import cn.aitstone.service.mapper.ProductMapper;
import cn.aitstone.service.mapper.ProductOnlineAuditLogMapper;
import cn.aitstone.service.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sheldon
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;


    @Override
    public void save(Product product) {
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.save(detail);

    }
    
    @Override
    public void onsale(List<Long> ids, Long eId) {
        for (Long id : ids){
            // 得到每个服务基本信息对象
            Product product = productMapper.loadById(id);
            StringBuffer sb = new StringBuffer();
            // 调用百度AI进行文字，图片的审核
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                sb.append("服务名称不合法，服务名称为：" + product.getName());
            }
            String[] arr = product.getResources().split(",");
            for (String ar : arr){
                if (!ImgCensor.ImgCensor(ar)){
                    sb.append("服务图片不合法，服务图片路径：" + ar);
                }
            }
//            if (!ImgCensor.ImgCensor(product.getResources().split(",")[0])){
//                sb.append("宠物图片不合法，宠物图片路径：" + product.getResources().split(",")[0]);
//            }
            if (sb.length() > 0){
                // 如果违规，我们要保存审核失败日志
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setNote(sb.toString());
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setAuditId(eId);
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                continue;
            }
            productMapper.onsale(id);


        }
    }

    /**
    * @Title: offsale
    * @Description: 服务下架
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/21 9:52
    * @Parameters: [ids]
    * @Return void
    */
    @Override
    public void offsale(List<Long> ids) {
        productMapper.offsale(ids);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.update(detail);
    }


}
